#include "View.h"
#include "DirectX11.h"

View::View(void)
{
	cb = nullptr;
	ZeroMemory(&scene,sizeof(SCENE));
	ZeroMemory(&viewport,sizeof(D3D11_VIEWPORT));
}


View::~View(void)
{
	SAFE_RELEASE(cb);
}

void View::Initialize(){
	ID3D11Device* device = DirectX11::GetInstance()->GetDevice();

	D3D11_BUFFER_DESC desc;
	ZeroMemory(&desc, sizeof(D3D11_BUFFER_DESC));
	desc.Usage = D3D11_USAGE_DYNAMIC;
	desc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
	desc.MiscFlags = 0;
	desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
	desc.ByteWidth = sizeof(SCENE);
	desc.StructureByteStride = 0;

	D3D11_SUBRESOURCE_DATA init_data;
	ZeroMemory(&init_data, sizeof(D3D11_SUBRESOURCE_DATA));
	init_data.pSysMem = &scene;
	init_data.SysMemPitch = 0;
	init_data.SysMemSlicePitch = 0;

	desc.ByteWidth = sizeof(SCENE);
	init_data.pSysMem = &scene;

	HRESULT hr = device->CreateBuffer(&desc, &init_data, &cb);
	ERRORCHECK(hr, L"ERROR: Failed to create Object CB");

	XMVECTOR camera_position = XMVectorSet(0.0f, 5.0f, -4.0f, 1.0f);
	XMVECTOR camera_up = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f);
	XMVECTOR camera_target = XMVectorSet(0.0f, 0.0f, 0.0f, 1.0f);
	XMMATRIX camera_view = XMMatrixLookAtLH(camera_position, camera_target, camera_up);

	float aspect = (float)BACKBUFFER_WIDTH / BACKBUFFER_HEIGHT;
	XMMATRIX projection = XMMatrixPerspectiveFovLH(XMConvertToRadians(65), aspect, 0.1f, 1000.0f);
	XMStoreFloat4x4(&scene.mView, camera_view);
	XMStoreFloat4x4(&scene.mProj, projection);
}

void View::UpdateStruct(SCENE& obj){
	memcpy(&scene,&obj,sizeof(SCENE));
}


